package com.hand.demo.api.controller.v1;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import org.hzero.boot.platform.profile.ProfileClient;
import org.hzero.core.base.BaseController;
import org.hzero.core.util.Results;
import com.hand.demo.app.service.ExampleService;
import com.hand.demo.config.SwaggerTags;
import com.hand.demo.domain.entity.Example;
import com.hand.demo.domain.repository.ExampleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * API接口
 */
@Api(tags = SwaggerTags.EXAMPLE)
@RestController("exampleController.v1")
@RequestMapping("/v1/example")
public class ExampleController extends BaseController {

    @Autowired
    private ExampleService exampleService;
    @Autowired
    private ExampleRepository exampleRepository;
    @Autowired
    private ProfileClient profileClient;

    @ApiOperation(value = "根据ID获取")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", paramType = "path")
    })
    @GetMapping("/{id}")
    public ResponseEntity<Example> hello(@PathVariable Long id) {
        return Results.success(exampleRepository.selectByPrimaryKey(id));
    }

    @ApiOperation(value = "获取全部")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "all", value = "all", paramType = "path")
    })
    @GetMapping("/all")
    public ResponseEntity<List<Example>> all() {
        return Results.success(exampleRepository.selectAll());
    }

    @ApiOperation(value = "根据ID修改")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "update", value = "update", paramType = "path")
    })
    @PutMapping("/update")
    public ResponseEntity<Integer> example(@RequestBody Example example){
        return Results.success(exampleRepository.updateByPrimaryKey(example));
    }

    @ApiOperation(value = "根据ID删除")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "delete", value = "delete", paramType = "path")
    })
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Integer> example1(@PathVariable Long id){
        return Results.success(exampleRepository.deleteByPrimaryKey(id));
    }

    @ApiOperation(value = "新增")
    @Permission(level = ResourceLevel.SITE, permissionLogin = true)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "insert", value = "insert", paramType = "path")
    })
    @PostMapping("/insert")
    public ResponseEntity<Integer> example2(@RequestBody Example example){
        return Results.success(exampleRepository.insert(example));
    }


}